from typing import Optional

from app.enums.result_code import ResultCode


class AppException(Exception):
    """应用异常基类"""

    def __init__(
        self, message: Optional[str] = None, code: Optional[str] = None, result_code: Optional[ResultCode] = None
    ):
        """
        初始化应用异常

        Args:
            code: 错误码
            message: 错误消息
            result_code: Optional[ResultCode] = None
        """
        self.code = result_code.code if result_code else (code or ResultCode.SYSTEM_ERROR.code)
        self.msg = result_code.label if result_code else (message or ResultCode.SYSTEM_ERROR.label)
        super().__init__(message)


class OptimisticLockError(AppException):
    """乐观锁冲突异常"""

    pass


class CRUDError(AppException):
    """CRUD 操作通用异常"""

    pass
